home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / file / logiso.000 / logiso / RCS / inode.c,v < prev    next >
Encoding:
Text File  |  1995-03-24  |  20.1 KB  |  789 lines

  1. head    1.5;
  2. access;
  3. symbols
  4.     VER_0_3:1.5
  5.     VER_0_2:1.4;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 1.5
  11. date    95.03.24.11.44.17;    author coulter;    state Exp;
  12. branches;
  13. next    1.4;
  14.  
  15. 1.4
  16. date    95.03.10.02.43.06;    author coulter;    state Exp;
  17. branches;
  18. next    1.3;
  19.  
  20. 1.3
  21. date    95.02.18.09.21.50;    author coulter;    state Exp;
  22. branches;
  23. next    1.2;
  24.  
  25. 1.2
  26. date    95.02.13.20.56.00;    author coulter;    state Exp;
  27. branches;
  28. next    1.1;
  29.  
  30. 1.1
  31. date    95.02.11.15.49.31;    author coulter;    state Exp;
  32. branches;
  33. next    ;
  34.  
  35.  
  36. desc
  37. @inode.c - add logging call
  38. @
  39.  
  40.  
  41. 1.5
  42. log
  43. @Checkin version for 0.3 distribution.
  44. @
  45. text
  46. @/*
  47.  *  linux/fs/isofs/inode.c
  48.  * 
  49.  *  Copyright 1995 Michael Coulter for access logging.
  50.  *
  51.  *  (C) 1992, 1993, 1994  Eric Youngdale Modified for ISO9660 filesystem.
  52.  *
  53.  *  (C) 1991  Linus Torvalds - minix filesystem
  54.  */
  55.  
  56. #include <linux/stat.h>
  57. #include <linux/sched.h>
  58. #include <linux/iso_fs.h>
  59. #include <linux/kernel.h>
  60. #include <linux/major.h>
  61. #include <linux/mm.h>
  62. #include <linux/string.h>
  63. #include <linux/locks.h>
  64. #include <linux/malloc.h>
  65. #include <linux/errno.h>
  66.  
  67. #include <asm/system.h>
  68. #include <asm/segment.h>
  69.  
  70. #ifdef LEAK_CHECK
  71. static int check_malloc = 0;
  72. static int check_bread = 0;
  73. #endif
  74.  
  75. void isofs_put_super(struct super_block *sb)
  76. {
  77.     lock_super(sb);
  78.  
  79. #ifdef LEAK_CHECK
  80.     printk("Outstanding mallocs:%d, outstanding buffers: %d\n", 
  81.            check_malloc, check_bread);
  82. #endif
  83.     sb->s_dev = 0;
  84.     unlock_super(sb);
  85.     return;
  86. }
  87.  
  88. static struct super_operations isofs_sops = { 
  89.     isofs_read_inode,
  90.     NULL,            /* notify_change */
  91.     NULL,            /* write_inode */
  92.     NULL,            /* put_inode */
  93.     isofs_put_super,
  94.     NULL,            /* write_super */
  95.     isofs_statfs,
  96.     NULL
  97. };
  98.  
  99. struct iso9660_options{
  100.   char map;
  101.   char rock;
  102.   char cruft;
  103.   unsigned char conversion;
  104.   unsigned int blocksize;
  105.   gid_t gid;
  106.   uid_t uid;
  107. };
  108.  
  109. static int parse_options(char *options, struct iso9660_options * popt)
  110. {
  111.     char *this_char,*value;
  112.  
  113.     popt->map = 'n';
  114.     popt->rock = 'y';
  115.     popt->cruft = 'n';
  116.     popt->conversion = 'a';
  117.     popt->blocksize = 1024;
  118.     popt->gid = 0;
  119.     popt->uid = 0;
  120.     if (!options) return 1;
  121.     for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
  122.             if (strncmp(this_char,"norock",6) == 0) {
  123.           popt->rock = 'n';
  124.           continue;
  125.         };
  126.             if (strncmp(this_char,"cruft",5) == 0) {
  127.           popt->cruft = 'y';
  128.           continue;
  129.         };
  130.         if ((value = strchr(this_char,'=')) != NULL)
  131.             *value++ = 0;
  132.         if (!strcmp(this_char,"map") && value) {
  133.             if (value[0] && !value[1] && strchr("on",*value))
  134.                 popt->map = *value;
  135.             else if (!strcmp(value,"off")) popt->map = 'o';
  136.             else if (!strcmp(value,"normal")) popt->map = 'n';
  137.             else return 0;
  138.         }
  139.         else if (!strcmp(this_char,"conv") && value) {
  140.             if (value[0] && !value[1] && strchr("bta",*value))
  141.                 popt->conversion = *value;
  142.             else if (!strcmp(value,"binary")) popt->conversion = 'b';
  143.             else if (!strcmp(value,"text")) popt->conversion = 't';
  144.             else if (!strcmp(value,"mtext")) popt->conversion = 'm';
  145.             else if (!strcmp(value,"auto")) popt->conversion = 'a';
  146.             else return 0;
  147.         }
  148.         else if (value && 
  149.              (!strcmp(this_char,"block") ||
  150.               !strcmp(this_char,"uid") ||
  151.               !strcmp(this_char,"gid"))) {
  152.           char * vpnt = value;
  153.           unsigned int ivalue;
  154.           ivalue = 0;
  155.           while(*vpnt){
  156.             if(*vpnt <  '0' || *vpnt > '9') break;
  157.             ivalue = ivalue * 10 + (*vpnt - '0');
  158.             vpnt++;
  159.           };
  160.           if (*vpnt) return 0;
  161.           switch(*this_char) {
  162.           case 'b':
  163.             if (ivalue != 1024 && ivalue != 2048) return 0;
  164.             popt->blocksize = ivalue;
  165.             break;
  166.           case 'g':
  167.             popt->uid = ivalue;
  168.             break;
  169.           case 'u':
  170.             popt->gid = ivalue;
  171.             break;
  172.           }
  173.         }
  174.         else return 0;
  175.     }
  176.     return 1;
  177. }
  178.  
  179. struct super_block *isofs_read_super(struct super_block *s,void *data,
  180.                      int silent)
  181. {
  182.     struct buffer_head *bh;
  183.     int iso_blknum;
  184.     unsigned int blocksize_bits;
  185.     int high_sierra;
  186.     int dev=s->s_dev;
  187.     struct iso_volume_descriptor *vdp;
  188.     struct hs_volume_descriptor *hdp;
  189.  
  190.     struct iso_primary_descriptor *pri = NULL;
  191.     struct hs_primary_descriptor *h_pri = NULL;
  192.  
  193.     struct iso_directory_record *rootp;
  194.  
  195.     struct iso9660_options opt;
  196.  
  197.     if (!parse_options((char *) data,&opt)) {
  198.         s->s_dev = 0;
  199.         return NULL;
  200.     }
  201.  
  202. #if 0
  203.     printk("map = %c\n", opt.map);
  204.     printk("rock = %c\n", opt.rock);
  205.     printk("cruft = %c\n", opt.cruft);
  206.     printk("conversion = %c\n", opt.conversion);
  207.     printk("blocksize = %d\n", opt.blocksize);
  208.     printk("gid = %d\n", opt.gid);
  209.     printk("uid = %d\n", opt.uid);
  210. #endif
  211.     
  212.     blocksize_bits = 0;
  213.     {
  214.       int i = opt.blocksize;
  215.       while (i != 1){
  216.         blocksize_bits++;
  217.         i >>=1;
  218.       };
  219.     };
  220.     set_blocksize(dev, opt.blocksize);
  221.  
  222.     lock_super(s);
  223.  
  224.     s->u.isofs_sb.s_high_sierra = high_sierra = 0; /* default is iso9660 */
  225.  
  226.     for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
  227.         if (!(bh = bread(dev, iso_blknum << (ISOFS_BLOCK_BITS-blocksize_bits), opt.blocksize))) {
  228.             s->s_dev=0;
  229.             printk("isofs_read_super: bread failed, dev 0x%x iso_blknum %d\n",
  230.                    dev, iso_blknum);
  231.             unlock_super(s);
  232.             return NULL;
  233.         }
  234.  
  235.         vdp = (struct iso_volume_descriptor *)bh->b_data;
  236.         hdp = (struct hs_volume_descriptor *)bh->b_data;
  237.  
  238.         
  239.         if (strncmp (hdp->id, HS_STANDARD_ID, sizeof hdp->id) == 0) {
  240.           if (isonum_711 (hdp->type) != ISO_VD_PRIMARY)
  241.             goto out;
  242.           if (isonum_711 (hdp->type) == ISO_VD_END)
  243.                 goto out;
  244.         
  245.                 s->u.isofs_sb.s_high_sierra = 1;
  246.             high_sierra = 1;
  247.                 opt.rock = 'n';
  248.                 h_pri = (struct hs_primary_descriptor *)vdp;
  249.             break;
  250.         };
  251.         
  252.         if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
  253.           if (isonum_711 (vdp->type) != ISO_VD_PRIMARY)
  254.             goto out;
  255.           if (isonum_711 (vdp->type) == ISO_VD_END)
  256.             goto out;
  257.         
  258.                 pri = (struct iso_primary_descriptor *)vdp;
  259.             break;
  260.             };
  261.  
  262.         brelse(bh);
  263.           }
  264.     if(iso_blknum == 100) {
  265.         if (!silent)
  266.             printk("Unable to identify CD-ROM format.\n");
  267.         s->s_dev = 0;
  268.         unlock_super(s);
  269.         return NULL;
  270.     };
  271.     
  272.     
  273.     if(high_sierra){
  274.       rootp = (struct iso_directory_record *) h_pri->root_directory_record;
  275.       if (isonum_723 (h_pri->volume_set_size) != 1) {
  276.         printk("Multi-volume disks not (yet) supported.\n");
  277.         goto out;
  278.       };
  279.       s->u.isofs_sb.s_nzones = isonum_733 (h_pri->volume_space_size);
  280.       s->u.isofs_sb.s_log_zone_size = isonum_723 (h_pri->logical_block_size);
  281.       s->u.isofs_sb.s_max_size = isonum_733(h_pri->volume_space_size);
  282.     } else {
  283.       rootp = (struct iso_directory_record *) pri->root_directory_record;
  284.       if (isonum_723 (pri->volume_set_size) != 1) {
  285.         printk("Multi-volume disks not (yet) supported.\n");
  286.         goto out;
  287.       };
  288.       s->u.isofs_sb.s_nzones = isonum_733 (pri->volume_space_size);
  289.       s->u.isofs_sb.s_log_zone_size = isonum_723 (pri->logical_block_size);
  290.       s->u.isofs_sb.s_max_size = isonum_733(pri->volume_space_size);
  291.     }
  292.     
  293.     s->u.isofs_sb.s_ninodes = 0; /* No way to figure this out easily */
  294.     
  295.     s->u.isofs_sb.s_firstdatazone = isonum_733( rootp->extent) << 
  296.         (ISOFS_BLOCK_BITS - blocksize_bits);
  297.     s->s_magic = ISOFS_SUPER_MAGIC;
  298.     
  299.     /* The CDROM is read-only, has no nodes (devices) on it, and since
  300.        all of the files appear to be owned by root, we really do not want
  301.        to allow suid.  (suid or devices will not show up unless we have
  302.        Rock Ridge extensions) */
  303.     
  304.     s->s_flags |= MS_RDONLY /* | MS_NODEV | MS_NOSUID */;
  305.     
  306.     if(s->u.isofs_sb.s_log_zone_size != (1 << ISOFS_BLOCK_BITS)) {
  307.         printk("1 <<Block bits != Block size\n");
  308.         goto out;
  309.     };
  310.     
  311.     brelse(bh);
  312.     
  313.     printk("Max size:%ld   Log zone size:%ld\n",
  314.            s->u.isofs_sb.s_max_size, 
  315.            s->u.isofs_sb.s_log_zone_size);
  316.     printk("First datazone:%ld   Root inode number %d\n",
  317.            s->u.isofs_sb.s_firstdatazone,
  318.            isonum_733 (rootp->extent) << ISOFS_BLOCK_BITS);
  319.     if(high_sierra) printk("Disc in High Sierra format.\n");
  320.     unlock_super(s);
  321.     /* set up enough so that it can read an inode */
  322.     
  323.     s->s_dev = dev;
  324.     s->s_op = &isofs_sops;
  325.     s->u.isofs_sb.s_mapping = opt.map;
  326.     s->u.isofs_sb.s_rock = (opt.rock == 'y' ? 1 : 0);
  327.     s->u.isofs_sb.s_conversion = opt.conversion;
  328.     s->u.isofs_sb.s_cruft = opt.cruft;
  329.     s->u.isofs_sb.s_uid = opt.uid;
  330.     s->u.isofs_sb.s_gid = opt.gid;
  331.     s->s_blocksize = opt.blocksize;
  332.     s->s_blocksize_bits = blocksize_bits;
  333.     s->s_mounted = iget(s, isonum_733 (rootp->extent) << ISOFS_BLOCK_BITS);
  334.     unlock_super(s);
  335.  
  336.     if (!(s->s_mounted)) {
  337.         s->s_dev=0;
  338.         printk("get root inode failed\n");
  339.         return NULL;
  340.     }
  341.  
  342.     if(!check_disk_change(s->s_dev)) return s;
  343.  out: /* Kick out for various error conditions */
  344.     brelse(bh);
  345.     s->s_dev = 0;
  346.     unlock_super(s);
  347.     return NULL;
  348. }
  349.  
  350. void isofs_statfs (struct super_block *sb, struct statfs *buf)
  351. {
  352.     put_fs_long(ISOFS_SUPER_MAGIC, &buf->f_type);
  353.     put_fs_long(1 << ISOFS_BLOCK_BITS, &buf->f_bsize);
  354.     put_fs_long(sb->u.isofs_sb.s_nzones, &buf->f_blocks);
  355.     put_fs_long(0, &buf->f_bfree);
  356.     put_fs_long(0, &buf->f_bavail);
  357.     put_fs_long(sb->u.isofs_sb.s_ninodes, &buf->f_files);
  358.     put_fs_long(0, &buf->f_ffree);
  359.     put_fs_long(NAME_MAX, &buf->f_namelen);
  360.     /* Don't know what value to put in buf->f_fsid */
  361. }
  362.  
  363. int isofs_bmap(struct inode * inode,int block)
  364. {
  365.  
  366.     /* Reduce log volume by not logging this.  it is 
  367.     ** always? preceeded by other ops. 
  368.     */
  369.     /* iso_log_add(inode->i_ino, inode->i_dev, ISO_LOG_OP_isofs_bmap); */
  370.     if (block<0) {
  371.         printk("_isofs_bmap: block<0");
  372.         return 0;
  373.     }
  374.     return inode->u.isofs_i.i_first_extent + block;
  375. }
  376.  
  377. void isofs_read_inode(struct inode * inode)
  378. {
  379.     unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
  380.     struct buffer_head * bh;
  381.     struct iso_directory_record * raw_inode;
  382.     unsigned char *pnt = NULL;
  383.     void *cpnt = NULL;
  384.     int high_sierra;
  385.     int block;
  386.     int i;
  387.  
  388.     block = inode->i_ino >> ISOFS_BUFFER_BITS(inode);
  389.     if (!(bh=bread(inode->i_dev,block, bufsize))) {
  390.       printk("unable to read i-node block");
  391.       goto fail;
  392.     }
  393.     
  394.     pnt = ((unsigned char *) bh->b_data
  395.            + (inode->i_ino & (bufsize - 1)));
  396.     raw_inode = ((struct iso_directory_record *) pnt);
  397.     high_sierra = inode->i_sb->u.isofs_sb.s_high_sierra;
  398.  
  399.     if ((inode->i_ino & (bufsize - 1)) + *pnt > bufsize){
  400.             int frag1, offset;
  401.  
  402.         offset = (inode->i_ino & (bufsize - 1));
  403.         frag1 = bufsize - offset;
  404.             cpnt = kmalloc(*pnt,GFP_KERNEL);
  405.         if (cpnt == NULL) {
  406.             printk(KERN_INFO "NoMem ISO inode %lu\n",inode->i_ino);
  407.             brelse(bh);
  408.             goto fail;
  409.         }
  410.         memcpy(cpnt, bh->b_data + offset, frag1);
  411.         brelse(bh);
  412.         if (!(bh = bread(inode->i_dev,++block, bufsize))) {
  413.             kfree(cpnt);
  414.             printk("unable to read i-node block");
  415.             goto fail;
  416.         }
  417.         offset += *pnt - bufsize;
  418.         memcpy((char *)cpnt+frag1, bh->b_data, offset);
  419.         pnt = ((unsigned char *) cpnt);
  420.         raw_inode = ((struct iso_directory_record *) pnt);
  421.     }
  422.  
  423.     inode->i_mode = S_IRUGO; /* Everybody gets to read the file. */
  424.     inode->i_nlink = 1;
  425.     
  426.     if (raw_inode->flags[-high_sierra] & 2) {
  427.         inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR;
  428.         inode->i_nlink = 1; /* Set to 1.  We know there are 2, but
  429.                        the find utility tries to optimize
  430.                        if it is 2, and it screws up.  It is
  431.                        easier to give 1 which tells find to
  432.                        do it the hard way. */
  433.     } else {
  434.         inode->i_mode = S_IRUGO; /* Everybody gets to read the file. */
  435.         inode->i_nlink = 1;
  436.             inode->i_mode |= S_IFREG;
  437. /* If there are no periods in the name, then set the execute permission bit */
  438.         for(i=0; i< raw_inode->name_len[0]; i++)
  439.             if(raw_inode->name[i]=='.' || raw_inode->name[i]==';')
  440.                 break;
  441.         if(i == raw_inode->name_len[0] || raw_inode->name[i] == ';') 
  442.             inode->i_mode |= S_IXUGO; /* execute permission */
  443.     }
  444.     inode->i_uid = inode->i_sb->u.isofs_sb.s_uid;
  445.     inode->i_gid = inode->i_sb->u.isofs_sb.s_gid;
  446.     inode->i_size = isonum_733 (raw_inode->size);
  447.  
  448.     /* There are defective discs out there - we do this to protect
  449.        ourselves.  A cdrom will never contain more than 700Mb */
  450.     if((inode->i_size < 0 || inode->i_size > 700000000) &&
  451.         inode->i_sb->u.isofs_sb.s_cruft == 'n') {
  452.       printk("Warning: defective cdrom.  Enabling \"cruft\" mount option.\n");
  453.       inode->i_sb->u.isofs_sb.s_cruft = 'y';
  454.     }
  455.  
  456. /* Some dipshit decided to store some other bit of information in the high
  457.    byte of the file length.  Catch this and holler.  WARNING: this will make
  458.    it impossible for a file to be > 16Mb on the CDROM!!!*/
  459.  
  460.     if(inode->i_sb->u.isofs_sb.s_cruft == 'y' && 
  461.        inode->i_size & 0xff000000){
  462. /*      printk("Illegal format on cdrom.  Pester manufacturer.\n"); */
  463.       inode->i_size &= 0x00ffffff;
  464.     }
  465.     
  466.     if (raw_inode->interleave[0]) {
  467.         printk("Interleaved files not (yet) supported.\n");
  468.         inode->i_size = 0;
  469.     }
  470.  
  471.     /* I have no idea what file_unit_size is used for, so
  472.        we will flag it for now */
  473.     if(raw_inode->file_unit_size[0] != 0){
  474.         printk("File unit size != 0 for ISO file (%ld).\n",inode->i_ino);
  475.     }
  476.  
  477.     /* I have no idea what other flag bits are used for, so
  478.        we will flag it for now */
  479.     if((raw_inode->flags[-high_sierra] & ~2)!= 0){
  480.         printk("Unusual flag settings for ISO file (%ld %x).\n",
  481.                inode->i_ino, raw_inode->flags[-high_sierra]);
  482.     }
  483.  
  484. #ifdef DEBUG
  485.     printk("Get inode %d: %d %d: %d\n",inode->i_ino, block, 
  486.            ((int)pnt) & 0x3ff, inode->i_size);
  487. #endif
  488.     
  489.     inode->i_mtime = inode->i_atime = inode->i_ctime = 
  490.       iso_date(raw_inode->date, high_sierra);
  491.  
  492.     inode->u.isofs_i.i_first_extent = 
  493.       (isonum_733 (raw_inode->extent) + 
  494.        isonum_711 (raw_inode->ext_attr_length)) << 
  495.         (ISOFS_BLOCK_BITS - ISOFS_BUFFER_BITS(inode));
  496.     
  497.     inode->u.isofs_i.i_backlink = 0xffffffff; /* Will be used for previous directory */
  498.     switch (inode->i_sb->u.isofs_sb.s_conversion){
  499.     case 'a':
  500.       inode->u.isofs_i.i_file_format = ISOFS_FILE_UNKNOWN; /* File type */
  501.       break;
  502.     case 'b':
  503.       inode->u.isofs_i.i_file_format = ISOFS_FILE_BINARY; /* File type */
  504.       break;
  505.     case 't':
  506.       inode->u.isofs_i.i_file_format = ISOFS_FILE_TEXT; /* File type */
  507.       break;
  508.     case 'm':
  509.       inode->u.isofs_i.i_file_format = ISOFS_FILE_TEXT_M; /* File type */
  510.       break;
  511.     }
  512.  
  513. /* Now test for possible Rock Ridge extensions which will override some of
  514.    these numbers in the inode structure. */
  515.  
  516.     if (!high_sierra)
  517.       parse_rock_ridge_inode(raw_inode, inode);
  518.     
  519. #ifdef DEBUG
  520.     printk("Inode: %x extent: %x\n",inode->i_ino, inode->u.isofs_i.i_first_extent);
  521. #endif
  522.     brelse(bh);
  523.     
  524.     inode->i_op = NULL;
  525.  
  526.     /* A volume number of 0 is nonsense.  Disable checking if we see
  527.        this */
  528.     if (inode->i_sb->u.isofs_sb.s_cruft == 'n' && 
  529.         isonum_723 (raw_inode->volume_sequence_number) == 0) {
  530.       printk("Warning: defective cdrom.  Enabling \"cruft\" mount option.\n");
  531.       inode->i_sb->u.isofs_sb.s_cruft = 'y';
  532.     }
  533.  
  534.     if (inode->i_sb->u.isofs_sb.s_cruft != 'y' && 
  535.         isonum_723 (raw_inode->volume_sequence_number) != 1) {
  536.         printk("Multi volume CD somehow got mounted.\n");
  537.     } else {
  538.       if (S_ISREG(inode->i_mode))
  539.         inode->i_op = &isofs_file_inode_operations;
  540.       else if (S_ISDIR(inode->i_mode))
  541.         inode->i_op = &isofs_dir_inode_operations;
  542.       else if (S_ISLNK(inode->i_mode))
  543.         inode->i_op = &isofs_symlink_inode_operations;
  544.       else if (S_ISCHR(inode->i_mode))
  545.         inode->i_op = &chrdev_inode_operations;
  546.       else if (S_ISBLK(inode->i_mode))
  547.         inode->i_op = &blkdev_inode_operations;
  548.       else if (S_ISFIFO(inode->i_mode))
  549.         init_fifo(inode);
  550.     }
  551.     if (cpnt) {
  552.         kfree (cpnt);
  553.         cpnt = NULL;
  554.     }
  555.     return;
  556.       fail:
  557.     /* With a data error we return this information */
  558.     inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
  559.     inode->u.isofs_i.i_first_extent = 0;
  560.     inode->u.isofs_i.i_backlink = 0xffffffff;
  561.     inode->i_size = 0;
  562.     inode->i_nlink = 1;
  563.     inode->i_uid = inode->i_gid = 0;
  564.     inode->i_mode = S_IFREG;  /*Regular file, no one gets to read*/
  565.     inode->i_op = NULL;
  566.     return;
  567. }
  568.  
  569. /* There are times when we need to know the inode number of a parent of
  570.    a particular directory.  When control passes through a routine that
  571.    has access to the parent information, it fills it into the inode structure,
  572.    but sometimes the inode gets flushed out of the queue, and someone
  573.    remembers the number.  When they try to open up again, we have lost
  574.    the information.  The '..' entry on the disc points to the data area
  575.    for a particular inode, so we can follow these links back up, but since
  576.    we do not know the inode number, we do not actually know how large the
  577.    directory is.  The disc is almost always correct, and there is
  578.    enough error checking on the drive itself, but an open ended search
  579.    makes me a little nervous.
  580.  
  581.    The bsd iso filesystem uses the extent number for an inode, and this
  582.    would work really nicely for us except that the read_inode function
  583.    would not have any clean way of finding the actual directory record
  584.    that goes with the file.  If we had such info, then it would pay
  585.    to change the inode numbers and eliminate this function.
  586. */
  587.  
  588. int isofs_lookup_grandparent(struct inode * parent, int extent)
  589. {
  590.     unsigned long bufsize = ISOFS_BUFFER_SIZE(parent);
  591.     unsigned char bufbits = ISOFS_BUFFER_BITS(parent);
  592.     unsigned int block,offset;
  593.     int parent_dir, inode_number;
  594.     int old_offset;
  595.     void * cpnt = NULL;
  596.     int result;
  597.     int directory_size;
  598.     struct buffer_head * bh;
  599.     struct iso_directory_record * de;
  600.     
  601.     offset = 0;
  602.     block = extent << (ISOFS_BLOCK_BITS - bufbits);
  603.     if (!(bh = bread(parent->i_dev, block, bufsize)))  return -1;
  604.     
  605.     while (1 == 1) {
  606.         de = (struct iso_directory_record *) (bh->b_data + offset);
  607.         if (*((unsigned char *) de) == 0) 
  608.         {
  609.             brelse(bh);
  610.             return -1;
  611.         }
  612.         
  613.         offset += *((unsigned char *) de);
  614.  
  615.         if (offset >= bufsize) 
  616.         {
  617.             printk(".. Directory not in first block"
  618.                    " of directory.\n");
  619.             brelse(bh);
  620.             return -1;
  621.         }
  622.         
  623.         if (de->name_len[0] == 1 && de->name[0] == 1) 
  624.         {
  625.             parent_dir = find_rock_ridge_relocation(de, parent);
  626.             directory_size = isonum_733 (de->size);
  627.             brelse(bh);
  628.             break;
  629.         }
  630.     }
  631. #ifdef DEBUG
  632.     printk("Parent dir:%x\n",parent_dir);
  633. #endif
  634.     /* Now we know the extent where the parent dir starts on. */
  635.     
  636.     result = -1;
  637.  
  638.     offset = 0;
  639.     block = parent_dir << (ISOFS_BLOCK_BITS - bufbits);
  640.     if (!block || !(bh = bread(parent->i_dev,block, bufsize)))
  641.         return -1;
  642.     
  643.     for(;;)
  644.     {
  645.         de = (struct iso_directory_record *) (bh->b_data + offset);
  646.         inode_number = (block << bufbits)+(offset & (bufsize - 1));
  647.         
  648.         /* If the length byte is zero, we should move on to the next
  649.            CDROM sector.  If we are at the end of the directory, we
  650.            kick out of the while loop. */
  651.         
  652.         if (*((unsigned char *) de) == 0) 
  653.         {
  654.             brelse(bh);
  655.             offset = 0;
  656.             block++;
  657.             directory_size -= bufsize;
  658.             if(directory_size < 0) return -1;
  659.             if((block & 1) && (ISOFS_BLOCK_BITS - bufbits))
  660.               return -1;
  661.             if (!block
  662.                 || !(bh = bread(parent->i_dev,block, bufsize)))
  663.                 return -1;
  664.             continue;
  665.         }
  666.         
  667.         /* Make sure that the entire directory record is in the current
  668.            bh block.  If not, we malloc a buffer, and put the two
  669.            halves together, so that we can cleanly read the block.  */
  670.  
  671.         old_offset = offset;
  672.         offset += *((unsigned char *) de);
  673.  
  674.         if (offset >= bufsize)
  675.         {
  676.                  unsigned int frag1;
  677.              frag1 = bufsize - old_offset;
  678.              cpnt = kmalloc(*((unsigned char *) de),GFP_KERNEL);
  679.              memcpy(cpnt, bh->b_data + old_offset, frag1);
  680.              de = (struct iso_directory_record *) ((char *)cpnt);
  681.             brelse(bh);
  682.             offset -= bufsize;
  683.              directory_size -= bufsize;
  684.             if(directory_size < 0) return -1;
  685.             block++;
  686.             if(!(bh = bread(parent->i_dev,block,bufsize))) {
  687.                      kfree(cpnt);
  688.                 return -1;
  689.             };
  690.              memcpy((char *)cpnt+frag1, bh->b_data, offset);
  691.         }
  692.         
  693.         if (find_rock_ridge_relocation(de, parent) == extent){
  694.             result = inode_number;
  695.             goto out;
  696.         }
  697.         
  698.         if (cpnt) {
  699.             kfree(cpnt);
  700.             cpnt = NULL;
  701.         }
  702.     }
  703.  
  704.     /* We go here for any condition we cannot handle.
  705.        We also drop through to here at the end of the directory. */
  706.  
  707.  out:
  708.     if (cpnt) {
  709.             kfree(cpnt);
  710.         cpnt = NULL;
  711.     }
  712.     brelse(bh);
  713. #ifdef DEBUG
  714.     printk("Resultant Inode %d\n",result);
  715. #endif
  716.     return result;
  717. }
  718.     
  719. #ifdef LEAK_CHECK
  720. #undef malloc
  721. #undef free_s
  722. #undef bread
  723. #undef brelse
  724.  
  725. void * leak_check_malloc(unsigned int size){
  726.   void * tmp;
  727.   check_malloc++;
  728.   tmp = kmalloc(size, GFP_KERNEL);
  729.   return tmp;
  730. }
  731.  
  732. void leak_check_free_s(void * obj, int size){
  733.   check_malloc--;
  734.   return kfree_s(obj, size);
  735. }
  736.  
  737. struct buffer_head * leak_check_bread(int dev, int block, int size){
  738.   check_bread++;
  739.   return bread(dev, block, size);
  740. }
  741.  
  742. void leak_check_brelse(struct buffer_head * bh){
  743.   check_bread--;
  744.   return brelse(bh);
  745. }
  746.  
  747. #endif
  748. @
  749.  
  750.  
  751. 1.4
  752. log
  753. @Checkin files modified to make version 0.2
  754. @
  755. text
  756. @d4 2
  757. @
  758.  
  759.  
  760. 1.3
  761. log
  762. @Checkin version 0.1, size, get log, stop
  763. @
  764. text
  765. @d322 1
  766. a322 1
  767.     /* iso_log_add(inode->i_ino, ISO_LOG_OP_isofs_bmap); */
  768. @
  769.  
  770.  
  771. 1.2
  772. log
  773. @Checkin working version.  Try to make it dynamic now.
  774. @
  775. text
  776. @d319 3
  777. a321 1
  778.     /* Reduce log volume by not logging this.  it is always other ops */
  779. @
  780.  
  781.  
  782. 1.1
  783. log
  784. @Initial revision
  785. @
  786. text
  787. @d319 2
  788. @
  789.